package sparrowGui
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import sparrowGui.impl.IScrollPan;
	import sparrowGui.item.Item;
	import sparrowGui.skin.ScrollBarSkin;
	import sparrowGui.skin.TagsBtnSkin;
	import sparrowGui.utils.PubFun;
	
	/**
	 * List.as
	 * Pelephone
	 * version 0.9.5
	 * 垂直列表控件，列表项有经过变色属性，点击后变色
	 */
	public class VList extends RadioGroup implements IScrollPan
	{
		protected var _vScroll:VScrollBar;
		protected var maskDP:Sprite;
		
		protected var _scrollValue:Number;//暂存滚动条位置,当style.isSaveSelect为真时用到
		
		/**
		 * 构造列表组件
		 * @param data			数据，可以是一维或者二维的数据
		 * @param itemSkinCla	项皮肤 //此class必需继承Item类，否则会报错
		 * @param vars			此列表组件的绘制用的变量
		 * @param pageSkin		翻页按钮组件的皮肤,如果为null则列表无翻页
		 */
		public function VList(data:Object,itemSkinCla:Class=null,vars:Object=null)
		{
			vars = vars || SparrowGUI.getIns().listVar;
			var ssk:Sprite = vars.scrollSkin || new ScrollBarSkin();
			_vScroll = new VScrollBar(ssk);
			itemSkinCla = (itemSkinCla==null)?TagsBtnSkin:itemSkinCla;
			super(data,itemSkinCla,vars);
		}
		
		override protected function init() : void
		{
			super.init();
			maskDP = new Sprite();
			
			addChild(maskDP);
			addChild(_vScroll);
		}
		
		//添加滚动条事件
		override protected function addEvt():void
		{
			super.addEvt();

			// 自动高为真时没有滚动条则添加事件
			if(scrollDist>0 && !style.autoHeight){
				if(_vScroll.hasEventListener(Event.CHANGE)) return;
				_vScroll.addEventListener(Event.CHANGE,onScroll);
				addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			}else
				delEvt();
		}
		
		protected function delEvt():void
		{
			if(_vScroll) _vScroll.removeEventListener(Event.CHANGE,onScroll);
			removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
		}
		
		///////////////////////////////////
		// public methods
		///////////////////////////////////
		
		override protected function draw():void
		{
			super.draw();
			
			maskDP.x = contDP.x = style.paddingLeft;
			maskDP.y = contDP.y = style.paddingTop;
			
			var dist:Number = (contDP.height - maskDP.height);
			
			if(dist<0 || style.autoHeight){
				maskDP.visible = false;
				_vScroll.visible = false;
			}else{
				maskDP.visible = true;
				maskDP.graphics.clear();
				maskDP.graphics.beginFill(0xFFFF00);
				maskDP.graphics.drawRect(0,0,(style.width),(style.height-style.paddingTop-style.paddingBottom));
				maskDP.graphics.endFill();
	//			this.scrollRect = new Rectangle(0,0,style.width,style.height);
				
				activeScroll();
				addEvt();
			}
			
			if(!style.autoWidth) return;
			for(var i:int=0;i<contDP.numChildren;i++){
				var oItem:Item = contDP.getChildAt(i) as Item;
				if(!oItem) continue;
				oItem.width = style.width - (_vScroll.visible?_vScroll.width:0) - style.paddingLeft - style.paddingRight;
			}
		}
		
		// , step
		/**
		 * 激活滚动条
		 * @param step		表示每点一次滚动条移动的距离,为0表示移到一个项的距离
		 * @param disHeight	高度减去的值,用于方便VHList继承时有横向滚动条
		 */		
		protected function activeScroll(step:Number=0,disHeight:Number=0):void
		{
			_vScroll.width = VScrollBar.THICKNESS;
			_vScroll.height = style.height - disHeight;
			// 通过遮罩激活滚动条
			if(!_itemsData) return;
			step = step || (contDP.height+style.spacing)/_itemsData.length;
			var stepValue:Number = step/(contDP.height-maskDP.height);
			PubFun.alignRect(maskDP,contDP,"TL");
			contDP.mask = maskDP;
			_vScroll.setSliderParams(maskDP.height,contDP.height,stepValue);
//			_vScroll.activeScroll(maskDP,contDP,step);
			maskDP.scaleX = 1;
			// 未滚动条未激活状态,autoHide为0,或者2则将其隐藏
			if(!_vScroll.enabled && !style.autoHide){
				_vScroll.visible = false;
				return;
			}
			maskDP.width = style.width - _vScroll.width;
			// 激活滚动条后才知道其是否显示,而后设置位置
			_vScroll.x = style.width - _vScroll.width;
			_vScroll.y = 0;
			// 设置上次保存的滚动条信息
			if(style.isSaveSelect && _scrollValue){
				_vScroll.value = _scrollValue;
				_vScroll.onMoveSlider(null);
			}
		}

		/**
		 * 创建列表数据.
		 */
		override protected function makeListItems():void
		{
			// 保存上次滚动条的位置
			if(!style.isSaveSelect) _selectedIndexs = [];
			else _scrollValue = _vScroll.value;
			
			super.makeListItems();
		}
		
		// 将item项排列子项的x,y值
		override protected function arrangeItems():void
		{
			PubFun.arrangeDisp(contDP,style.spacing);
		}
		
		// 比率参数为假或者不存在则按数组里字符的长度平均分配
		protected function oddsByDatas(datas:Object):Array
		{
			var sum:int = 0,td:String,i:int;
			if(!style.itemOdds){
				style.itemOdds = new Array();
				for(i=0;i<(datas as Array).length;i++){
					td = String(datas[i]);
					if(td) sum+=(td.length==0)?1:td.length;
				}
				for(i=0;i<(datas as Array).length;i++){
					td = String(datas[i]);
					style.itemOdds[i] = (((td.length==0)?1:td.length)/sum);
				}
			}
			return style.itemOdds;
		}
		
		
		///////////////////////////////////
		// event handlers
		///////////////////////////////////
		
		/**
		 * Called when the user scrolls the scroll bar.
		 */
		protected function onScroll(event:Event):void
		{
			if(!scrollDist) return;
			contDP.y = -_vScroll.value * scrollDist + maskDP.y;
		}
		
		/**
		 * Called when the mouse wheel is scrolled over the component.
		 */
		protected function onMouseWheel(event:MouseEvent):void
		{
			if(!scrollDist) return;
			_vScroll.value -= event.delta/scrollDist*2;
			_vScroll.onMoveSlider(null);
		}
		
		///////////////////////////////////
		// public
		///////////////////////////////////
		
		// comboBox用于判断是否点到滚动条
		public function isEventScrollBar(e:Event):Boolean
		{
			if(e.target==_vScroll.upBtn || e.target==_vScroll.downBtn || e.target==_vScroll.slider || e.target==_vScroll.skinbg)
				return true;
			else return false;
		}
		
/*		override public function update(data:Object):void
		{
			super.update(data);
		}*/
		
		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		
		/**
		 * 设置滚动到容器的index位置
		 * @param index
		 */		
		public function setScrollIndex(index:int):void
		{
			var oItem:Item = getItemByIndex(index);
			if(!oItem) return;
			var val:Number = (oItem.y) / (contDP.height - maskDP.height);
			val = val>1?1:val;
			_vScroll.value = val;
			_vScroll.onMoveSlider(null);
		}
		
		/**
		 * mask遮照项高
		 * @return 
		 */
		override public function get panHeight():Number
		{
			if(!style.autoHeight && style.height) return style.height;
			if(!itemsData || !itemsData.length) return 0;
			var itemHeight:int = style.rowHeight;
			if(!itemHeight){
				var item:DisplayObject;
				if(contDP.numChildren) item = contDP.getChildAt(0);
				else if(_itemSkinCla!=null) item = new _itemSkinCla() as Sprite;
				itemHeight = item?item.height:0;
			}
			var len:int = style.colNum?int(itemsData.length/style.colNum):itemsData.length;
			var padding:int = style.padding*2;
			return (itemHeight+spacing)*len + padding;
			
/*			if(contDP.height<style.height) return contDP.height;
			else return style.height;*/
		}
		
		/**
		 * 覆盖高属性
		 */
		override public function set height(h:Number):void
		{
			_vScroll.height = h;
			super.height = h;
		}
		
		// contDP和maskDP的差值
		public function get scrollDist():Number
		{
			return (contDP.height - maskDP.height);
		}
		
		/**
		 * 获取滚动条
		 * @return 
		 */
		public function get vScrollBar():VScrollBar
		{
			return _vScroll;
		}
		
		override public function dispose():void
		{
			removeChild(maskDP);
			maskDP = null;
			_vScroll.dispose();
			super.dispose();
		}
	}
}